import 'dart:convert';

import 'package:flutter_module/beans/base_bean.dart';
import 'package:flutter_module/beans/data_layer_bean.dart';
import 'package:flutter_module/beans/data_layer_item_selection_bean.dart';
import 'package:flutter_module/views/layerItemWidgets/single_selection_widget.dart';

enum DataLayerItemType {
  ///输入框
  dataLayerItemTypeInput,

  ///输入框集合
  dataLayerItemTypeInputArray,

  ///纯展示文本
  dataLayerItemTypeText,

  ///单选框
  dataLayerItemTypeSingleSelection,

  ///多选框
  dataLayerItemTypeMultipleSelection
}

class DataLayerItem extends BaseBean {
  ///对应的数据库字段名称
  String key;

  ///是不是主键
  bool primaryKey;

  ///控件类型
  DataLayerItemType type;

  ///用来显示控件名称
  String title;

  ///对应的值
  dynamic value;

  ///非必填项
  bool nullable;

  ///多选或单选的内容，根据类型处理
  List<dynamic>? selectOptions;

  ///是否是主名称属性,设置为true，地图上会已该字段内容渲染
  bool isMainName;

  String? describe;

  DataLayerItem({
    this.key = "",
    this.primaryKey = false,
    this.type = DataLayerItemType.dataLayerItemTypeInput,
    required this.title,
    this.value = "",
    this.nullable = true,
    this.selectOptions,
    this.isMainName = false,
    this.describe = "",
  });

  @override
  Map<String, dynamic> toMap() {
    String optionsJson = "";
    if (selectOptions != null) {
      switch (type) {
        case DataLayerItemType.dataLayerItemTypeSingleSelection:
          StringBuffer optionsJsonBuffer = StringBuffer();
          for (var item in selectOptions!) {
            if (optionsJsonBuffer.length > 0) {
              optionsJsonBuffer.write(",");
            }
            optionsJsonBuffer.write((item as SelectionBean).toJson());
          }
          optionsJson = '[${optionsJsonBuffer.toString()}]';
          break;
      }
    }
    return {
      "key": key,
      "primaryKey": primaryKey,
      "type": type.index,
      "title": title,
      "value": value,
      "nullable": nullable,
      "selectOptions": optionsJson,
      "isMainName": isMainName,
      "describe": describe,
    };
  }

  factory DataLayerItem.from(Map map) {
    DataLayerItemType type = DataLayerItemType.dataLayerItemTypeInput;
    if (map.containsKey("type")) {
      type = DataLayerItemType.values.elementAt(map["type"]);
    }
    List<dynamic>? options;
    if (map.containsKey("selectOptions")) {
      String optionsJson = map["selectOptions"];
      switch (type) {
        case DataLayerItemType.dataLayerItemTypeSingleSelection:
          List<dynamic> list = jsonDecode(optionsJson);
          if (list.isNotEmpty) {
            options = [];
          }
          for (var item in list) {
            if (item is Map) {
              options?.add(SelectionBean.from(item));
            }
          }
          break;
      }
    }

    return DataLayerItem(
      key: map["key"],
      primaryKey: map["primaryKey"] ?? false,
      type: DataLayerItemType.values.elementAt(map["type"] ?? 0),
      title: map["title"] ?? "",
      value: map["value"] ?? "",
      nullable: map["nullable"] ?? true,
      selectOptions: options,
      isMainName: map["isMainName"] ?? false,
      describe: map["describe"],
    );
  }

  ///复制一份新的数据
  DataLayerItem copy() {
    return DataLayerItem(
      key: key,
      primaryKey: primaryKey,
      type: type,
      title: title,
      value: value,
      nullable: nullable,
      selectOptions: selectOptions,
      isMainName: isMainName,
      describe: describe,
    );
  }

  ///他其他数据内容拷贝过来[key]不能修改
  void copyValues(DataLayerItem item) {
    primaryKey = item.primaryKey;
    type = item.type;
    title = item.title;
    value = item.value;
    nullable = item.nullable;
    selectOptions = item.selectOptions;
    isMainName = item.isMainName;
    describe = item.describe;
  }

  dynamic getSelectOptionTitle(dynamic value){
    if(selectOptions != null){
      switch(type){
        case DataLayerItemType.dataLayerItemTypeSingleSelection:
          for(var item in selectOptions!){
            if(value == (item as SelectionBean).value){
              return item.name;
            }
          }
          break;
      }
    }
    return "";
  }
}
